package com.xoa.service.workflow.customNumber;

import com.sun.org.apache.bcel.internal.generic.NEW;

import com.xoa.dao.customNumber.CustomNumberMapper;
import com.xoa.dao.customNumber.CustomNumberRecordMapper;
import com.xoa.model.customNumber.CustomNumber;
import com.xoa.model.customNumber.CustomNumberExample;
import com.xoa.model.customNumber.CustomNumberRecord;
import com.xoa.service.workflow.customNumber.wrapper.CustomNumberWrappers;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by gsb on 2017/8/9.
 */
@Service
public class CustomNumberService {
    @Resource
    private CustomNumberMapper customNumberMapper;
    @Resource
    private CustomNumberRecordMapper customNumberRecordMapper;
    @SuppressWarnings("all")
    public BaseWrapper generateCustomNumber(Integer uuid,String model,String modelId){
        CustomNumberWrappers   wrappers=new  CustomNumberWrappers();
        //获取对应model和modelId的记录，如果存在，则直接返回
        List<CustomNumberRecord> records = customNumberRecordMapper.queryCustomNumberRecordBymodel(String.valueOf(uuid),modelId);
        if(records.size()!=0){
            wrappers.setFlag(true);
            wrappers.setStatus(true);
            wrappers.setMsg("ok");
            wrappers.setData(records.get(0).getNumberStyle());
            return wrappers;
        }

        if(StringUtils.checkNull(String.valueOf(uuid))){
            wrappers.setFlag(false);
            wrappers.setStatus(true);
            wrappers.setMsg("传值为空");
            return wrappers;
      }
      CustomNumber customNumber= customNumberMapper.selectByPrimaryKey(uuid);
        String userSetStyle=customNumber.getSetStyle();
        String showStr="";
        int type=customNumber.getTypes();
        int numBit=customNumber.getNumberBit();
        int curNUmber=customNumber.getCurrentNumber()+1;
        int curNumBit =(curNUmber+"").length();
        Date date1=customNumber.getLastDate();
        Calendar date=Calendar.getInstance();
        date.setTime(date1);
      //  Calendar date = customNumber.getLastDate();//最后一次修改的日期
        Calendar now= Calendar.getInstance();//当前系统日期
        Date date2=now.getTime();
        if(type==1){             //自动累加
			/*if(year < 1900) year += 1900;*/
            //将字符串中所有的YYYY替换成当前年
            if (userSetStyle.indexOf("YYYY") > -1) {
                userSetStyle = userSetStyle.replaceAll("YYYY", now.get(Calendar.YEAR)+"");
            }
            if (userSetStyle.indexOf("YY") > -1) {
                userSetStyle = userSetStyle.replaceAll("YY", (now.get(Calendar.YEAR)+"").substring(2));
            }
            //将字符串中所有的MM替换成当前月
            if (userSetStyle.indexOf("MM") > -1) {
                int month = now.get(Calendar.MONTH)+1;
                String monthStr = "";
                if (month < 10) monthStr = "0";
                monthStr += month;
                userSetStyle = userSetStyle.replaceAll("MM", monthStr);
            }

            //将字符串中所有的DD替换成当前日
            if (userSetStyle.indexOf("DD") > -1) {
                int day = now.get(Calendar.DATE);
                String dayStr = "";
                if (day < 10) dayStr = "0";
                dayStr += day;
                userSetStyle = userSetStyle.replaceAll("DD", dayStr);
            }
            //将字符串中所有的##替换成编号
            if (userSetStyle.indexOf("##") > -1) {
                String num = "";
                for (int i = 0; i < numBit - curNumBit; i++) {
                    num += "0";
                }
                num +=curNUmber;
                userSetStyle =userSetStyle.replaceAll("##", num);
                curNUmber=Integer.parseInt(num);
            }
            showStr=userSetStyle;

        }

        if(type==2){           //按年累加
            int year=date.get(Calendar.YEAR);//最后一次修改日期的年份
            int nowYear=now.get(Calendar.YEAR);//当前年份
            if(year==nowYear){    //当年份不变时
                //将字符串中所有的YYYY替换成当前年
                if (userSetStyle.indexOf("YYYY") > -1) {
                    userSetStyle = userSetStyle.replaceAll("YYYY", nowYear+"");
                }
                if (userSetStyle.indexOf("YY") > -1) {
                    userSetStyle = userSetStyle.replaceAll("YY", (nowYear+"").substring(2));
                }
                //将字符串中所有的MM替换成当前月
                if (userSetStyle.indexOf("MM") > -1) {
                    int month = now.get(Calendar.MONTH)+1;
                    String monthStr = "";
                    if (month < 10) monthStr = "0";
                    monthStr += month;
                    userSetStyle = userSetStyle.replaceAll("MM", monthStr);
                }

                //将字符串中所有的DD替换成当前日
                if (userSetStyle.indexOf("DD") > -1) {
                    int day = now.get(Calendar.DATE);
                    String dayStr = "";
                    if (day < 10) dayStr = "0";
                    dayStr += day;
                    userSetStyle = userSetStyle.replaceAll("DD", dayStr);
                }
                //将字符串中所有的##替换成编号
                if (userSetStyle.indexOf("##") > -1) {
                    String num = "";
                    for (int i = 0; i < numBit - curNumBit; i++) {
                        num += "0";
                    }
                    num +=curNUmber;
                    userSetStyle =userSetStyle.replaceAll("##", num);
                    curNUmber=Integer.parseInt(num);
                }
                showStr=userSetStyle;
            }
            else{  //当年份改变时，编号置1累加
                //将字符串中所有的YYYY替换成当前年
                if (userSetStyle.indexOf("YYYY") > -1) {
                    userSetStyle = userSetStyle.replaceAll("YYYY", nowYear+"");
                }
                if (userSetStyle.indexOf("YY") > -1) {
                    userSetStyle = userSetStyle.replaceAll("YY", (nowYear+"").substring(2));
                }
                //将字符串中所有的MM替换成当前月
                if (userSetStyle.indexOf("MM") > -1) {
                    int month = now.get(Calendar.MONTH)+1;
                    String monthStr = "";
                    if (month < 10) monthStr = "0";
                    monthStr += month;
                    userSetStyle = userSetStyle.replaceAll("MM", monthStr);
                }

                //将字符串中所有的DD替换成当前日
                if (userSetStyle.indexOf("DD") > -1) {
                    int day = now.get(Calendar.DATE);
                    String dayStr = "";
                    if (day < 10) dayStr = "0";
                    dayStr += day;
                    userSetStyle = userSetStyle.replaceAll("DD", dayStr);
                }
                //将字符串中所有的##替换成编号
                if (userSetStyle.indexOf("##") > -1) {
                    String num = "";
                    curNUmber=1;
                    curNumBit =(curNUmber+"").length();
                    for (int i = 0; i < numBit - curNumBit; i++) {
                        num += "0";
                    }
                    num +=curNUmber;
                    userSetStyle =userSetStyle.replaceAll("##", num);
                    curNUmber=Integer.parseInt(num);
                }
                showStr=userSetStyle;
            }
        }
        if(type==3){  //按月累加
            int year=date.get(Calendar.YEAR);//最后一次修改日期年份
            int nowYear=now.get(Calendar.YEAR);//当前系统年份
            int months=date.get(Calendar.MONTH);//最后一次修改时间月
            int nowMonth=now.get(Calendar.MONTH);//当前系统月份
            if(year==nowYear&&months==nowMonth){   //年份和月份都不变的时候
                //将字符串中所有的YYYY替换成当前年
                if (userSetStyle.indexOf("YYYY") > -1) {
                    userSetStyle = userSetStyle.replaceAll("YYYY", nowYear+"");
                }
                if (userSetStyle.indexOf("YY") > -1) {
                    userSetStyle = userSetStyle.replaceAll("YY", (nowYear+"").substring(2));
                }
                //将字符串中所有的MM替换成当前月
                if (userSetStyle.indexOf("MM") > -1) {
                    int month = now.get(Calendar.MONTH)+1;
                    String monthStr = "";
                    if (month < 10) monthStr = "0";
                    monthStr += month;
                    userSetStyle = userSetStyle.replaceAll("MM", monthStr);
                }

                //将字符串中所有的DD替换成当前日
                if (userSetStyle.indexOf("DD") > -1) {
                    int day = now.get(Calendar.DATE);
                    String dayStr = "";
                    if (day < 10) dayStr = "0";
                    dayStr += day;
                    userSetStyle = userSetStyle.replaceAll("DD", dayStr);
                }
                //将字符串中所有的##替换成编号
                if (userSetStyle.indexOf("##") > -1) {
                    String num = "";
                    for (int i = 0; i < numBit - curNumBit; i++) {
                        num += "0";
                    }
                    num +=curNUmber;
                    userSetStyle =userSetStyle.replaceAll("##", num);
                    curNUmber=Integer.parseInt(num);
                }
                showStr=userSetStyle;

            }else{
                if(year!=nowYear||months!=nowMonth){
                    //将字符串中所有的YYYY替换成当前年
                    if (userSetStyle.indexOf("YYYY") > -1) {
                        userSetStyle = userSetStyle.replaceAll("YYYY", nowYear+"");
                    }
                    if (userSetStyle.indexOf("YY") > -1) {
                        userSetStyle = userSetStyle.replaceAll("YY", (nowYear+"").substring(2));
                    }
                    //将字符串中所有的MM替换成当前月
                    if (userSetStyle.indexOf("MM") > -1) {
                        int month = now.get(Calendar.MONTH)+1;
                        String monthStr = "";
                        if (month < 10) monthStr = "0";
                        monthStr += month;
                        userSetStyle = userSetStyle.replaceAll("MM", monthStr);
                    }

                    //将字符串中所有的DD替换成当前日
                    if (userSetStyle.indexOf("DD") > -1) {
                        int day = now.get(Calendar.DATE);
                        String dayStr = "";
                        if (day < 10) dayStr = "0";
                        dayStr += day;
                        userSetStyle = userSetStyle.replaceAll("DD", dayStr);
                    }
                    //将字符串中所有的##替换成编号
                    if (userSetStyle.indexOf("##") > -1) {
                        String num = "";
                        curNUmber=1;
                        curNumBit =(curNUmber+"").length();
                        for (int i = 0; i < numBit - curNumBit; i++) {
                            num += "0";
                        }
                        num +=curNUmber;
                        userSetStyle =userSetStyle.replaceAll("##", num);
                        curNUmber=Integer.parseInt(num);
                    }
                    showStr=userSetStyle;
                }
            }
        }

        if(type==4){
            int year=date.get(Calendar.YEAR);
            int nowYear=now.get(Calendar.YEAR);
            int months=date.get(Calendar.MONTH);
            int nowMonth=now.get(Calendar.MONTH);
            int days=date.get(Calendar.DATE);
            int nowDay = now.get(Calendar.DATE);
            if(year==nowYear&&months==nowMonth&&days==nowDay){
                //将字符串中所有的YYYY替换成当前年
                if (userSetStyle.indexOf("YYYY") > -1) {
                    userSetStyle = userSetStyle.replaceAll("YYYY", now.get(Calendar.YEAR)+"");
                }
                if (userSetStyle.indexOf("YY") > -1) {
                    userSetStyle = userSetStyle.replaceAll("YY", (now.get(Calendar.YEAR)+"").substring(2));
                }
                //将字符串中所有的MM替换成当前月
                if (userSetStyle.indexOf("MM") > -1) {
                    int month = now.get(Calendar.MONTH)+1;
                    String monthStr = "";
                    if (month < 10) monthStr = "0";
                    monthStr += month;
                    userSetStyle = userSetStyle.replaceAll("MM", monthStr);
                }

                //将字符串中所有的DD替换成当前日
                if (userSetStyle.indexOf("DD") > -1) {
                    int day = now.get(Calendar.DATE);
                    String dayStr = "";
                    if (day < 10) dayStr = "0";
                    dayStr += day;
                    userSetStyle = userSetStyle.replaceAll("DD", dayStr);
                }
                //将字符串中所有的##替换成编号
                if (userSetStyle.indexOf("##") > -1) {
                    String num = "";
                    for (int i = 0; i < numBit - curNumBit; i++) {
                        num += "0";
                    }
                    num +=curNUmber;
                    userSetStyle =userSetStyle.replaceAll("##", num);
                    curNUmber=Integer.parseInt(num);
                }
                showStr=userSetStyle;

            }else{
                if(year!=nowYear||months!=nowMonth||days!=nowDay){
                    //将字符串中所有的YYYY替换成当前年
                    if (userSetStyle.indexOf("YYYY") > -1) {
                        userSetStyle = userSetStyle.replaceAll("YYYY", nowYear+"");
                    }
                    if (userSetStyle.indexOf("YY") > -1) {
                        userSetStyle = userSetStyle.replaceAll("YY", (nowYear+"").substring(2));
                    }
                    //将字符串中所有的MM替换成当前月
                    if (userSetStyle.indexOf("MM") > -1) {
                        int month = now.get(Calendar.MONTH)+1;
                        String monthStr = "";
                        if (month < 10) monthStr = "0";
                        monthStr += month;
                        userSetStyle = userSetStyle.replaceAll("MM", monthStr);
                    }

                    //将字符串中所有的DD替换成当前日
                    if (userSetStyle.indexOf("DD") > -1) {
                        int day = now.get(Calendar.DATE);
                        String dayStr = "";
                        if (day < 10) dayStr = "0";
                        dayStr += day;
                        userSetStyle = userSetStyle.replaceAll("DD", dayStr);
                    }
                    //将字符串中所有的##替换成编号
                    if (userSetStyle.indexOf("##") > -1) {
                        String num = "";
                        curNUmber=1;
                        curNumBit =(curNUmber+"").length();
                        for (int i = 0; i < numBit - curNumBit; i++) {
                            num += "0";
                        }
                        num +=curNUmber;
                        userSetStyle =userSetStyle.replaceAll("##", num);
                        curNUmber=Integer.parseInt(num);
                    }
                    showStr=userSetStyle;
                }
            }
        }
        customNumber.setCurrentNumber(curNUmber);
        customNumber.setLastDate(date2);
        CustomNumberRecord customNumberRecord = new CustomNumberRecord();
        customNumberRecord.setCrTime(date2);
        customNumberRecord.setCustomNumberId(customNumber.getUuid());
        customNumberRecord.setNumberValue(curNUmber);
        customNumberRecord.setNumberStyle(showStr);
        customNumberRecord.setModel(model);
        customNumberRecord.setModelId(modelId);
        customNumberRecordMapper.insertSelective(customNumberRecord);
        customNumberRecord.setUuid(uuid);
        customNumberMapper.updateByPrimaryKey(customNumber);

        wrappers.setFlag(true);
        wrappers.setStatus(true);
        wrappers.setMsg("ok");
        wrappers.setData(showStr);
        return wrappers;

    }
    public BaseWrapper queryCustomNumber(HttpServletRequest request){
        CustomNumberWrappers   wrappers=new  CustomNumberWrappers();
        CustomNumberExample customNumberExample= new CustomNumberExample();
        List<CustomNumber> customNumbers= customNumberMapper.selectByExample(customNumberExample);
        wrappers.setFlag(true);
        wrappers.setStatus(true);
        wrappers.setMsg("ok");
        wrappers.setData(customNumbers);
        return wrappers;
    }

}
